package ch.unisi.inf.pfii.teamwhite.risk.graphic.mapEditor;

/**
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 */

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ch.unisi.inf.pfii.teamwhite.risk.graphic.menuFrame.MenuFrame;

public final class MapNameDialog extends JDialog{

	private String mapName;
	private JTextArea nameArea;
	
	public MapNameDialog(final MenuFrame menuFrame) {
		super(menuFrame, "Map Name", true);
		
		setLayout(new GridLayout(2, 1));
		add(nameTextArea());
		add(setAndQuit());
		
		setSize(200,150);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	    setResizable(false);
	    setVisible(true);
	}
	
	private final Component nameTextArea() {
		final JPanel namePanel = new JPanel();
		namePanel.setBorder(BorderFactory.createTitledBorder("Map Name"));
		
	    nameArea = new JTextArea("Please inserts a map name");
	    nameArea.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {
	    			nameArea.setText ("");
			}
		});
		namePanel.add(new JScrollPane(nameArea));
		add(namePanel);
		return namePanel;
	}
	
	private final Component setAndQuit(){
		final JPanel setAndQuit = new JPanel();
		setAndQuit.setLayout(new GridLayout(2, 1));
		
		setAndQuit.add(setName());
		setAndQuit.add(makeQuit());
		
		return setAndQuit;
	}
	
	private final Component setName() {
		final JButton setName = new JButton("Set Name");
		setName.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent ev) {
            	mapName = nameArea.getText();
            	setVisible(false);
            	new EditorFrame(MapNameDialog.this);
            }
		});
		add(setName);
		return setName;
	}
	
	private final Component makeQuit(){
		JButton quit = new JButton("Quit");
		quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		return quit;
	}
	
	public final String getMapName() {
		return mapName;
	}
}
